package com.slg.android.manyanime.fragment;


import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.CategoryActivity;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.adapter.AnimeCategoryAdapter;
import com.slg.android.manyanime.bean.AnimationCategory;
import com.slg.android.manyanime.callBackInterface.OnClickCallBack;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class AnimeCategoryFragment extends Fragment implements OnClickCallBack {
    private String json;
    @ViewInject(R.id.category_categories_recyclerView)
    private RecyclerView recyclerView;
    private List<AnimationCategory> data;
    private AnimeCategoryAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AssetManager assets = getActivity().getAssets();
        try {
            StringBuilder sb = new StringBuilder();
            InputStream inputStream = assets.open("channel_group_new.json");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            json = sb.toString();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_anime_category, container, false);
        ViewUtils.inject(this, view);
        try {
            data = AnimationCategory.parseCategory(new JSONArray(json));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (data != null && data.size() > 0) {
            GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
            adapter = new AnimeCategoryAdapter(getActivity(), data);
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int size = 1;
                    int type = adapter.getItemViewType(position);
                    if (type == adapter.TYPE_GROUP) {
                        size = 3;
                    } else {
                        size = 1;
                    }
                    return size;
                }
            });
            recyclerView.setLayoutManager(manager);
            adapter.setOnItemClickListener(this);
            recyclerView.setAdapter(adapter);
        }

        return view;
    }

    @Override
    public void onItemClick(View v, int position) {
        AnimationCategory category = data.get(position);
        Intent intent = new Intent(getActivity(), CategoryActivity.class);
        intent.putExtra("id", category.getId());
        startActivity(intent);
    }
}
